package com.example.spider.config;

import com.example.spider.proxy.SpiderContextHolder;
import org.springframework.http.converter.StringHttpMessageConverter;

import java.nio.charset.Charset;

public class AdapterStringHttpMessageConverter extends StringHttpMessageConverter {

    public AdapterStringHttpMessageConverter() {
    }

    public AdapterStringHttpMessageConverter(Charset defaultCharset) {
        super(defaultCharset);
    }

    public Charset getDefaultCharset() {
        // 先尝试采用当前任务指定的字符集、若未指定则根据请求判断
        Charset charset = SpiderContextHolder.getTask() != null ? SpiderContextHolder.getTask().getCharset() : null;
        return charset == null ? super.getDefaultCharset() : charset;
    }

}
